home *** CD-ROM | disk | FTP | other *** search
/ The Games Room / The Games Room.iso / amiga / card_games / spades / source / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  11.9 KB  |  427 lines

  1. /*
  2.    FILE: Input.c
  3.    PURPOSE: Routines for getting user input.
  4.    AUTHOR: Gregory M. Stelmack
  5. */
  6.  
  7. #ifndef GLOBALS_H
  8. #include "Globals.h"
  9. #endif
  10.  
  11. extern struct Menu *SpadesMenu;
  12.  
  13. struct EasyStruct    AboutES =
  14. {
  15.    sizeof(struct EasyStruct),
  16.    0,
  17.    "About Spades 2.12",
  18.    "Spades 2.12 by:\nGregory M. Stelmack\n8723 Del Rey Ct. #11-A\nTampa, FL 33617",
  19.    "OK"
  20. };
  21.  
  22. /**********************************************************
  23. * Function: ReadMouse                                     *
  24. * Parameters: none                                        *
  25. * Return Values: none                                     *
  26. * Purpose: Wait for mouse input, and update the mouse     *
  27. *          variables accordingly.                         *
  28. **********************************************************/
  29. void ReadMouse()
  30. {
  31.    ULONG class,menunum,itemnum,subnum;
  32.    USHORT code,selection,flags;
  33.    BOOL gotmouse;
  34.    BOOL QuitFlag,NewFlag;
  35.    struct IntuiMessage  *Message;
  36.    
  37.    /* Loop until we have a mouse message */
  38.  
  39.    gotmouse = FALSE;
  40.    QuitFlag = FALSE;
  41.    NewFlag = FALSE;
  42.      
  43.    while(!gotmouse)
  44.    {
  45.       /* Wait for Intuition Message */
  46.       
  47.       Wait(1L<<Wdw->UserPort->mp_SigBit);    
  48.         while(Message=(struct IntuiMessage *) GetMsg(Wdw->UserPort))
  49.         {
  50.          /* Interpret Message */
  51.     
  52.          class = Message->Class;
  53.          code = Message->Code;
  54.          ReplyMsg((struct Message *)Message);
  55.  
  56.          switch(class)
  57.          {
  58.             case MENUPICK:
  59.                selection = code;
  60.                while (selection != MENUNULL)
  61.                {
  62.                   menunum = MENUNUM(selection);
  63.                   itemnum = ITEMNUM(selection);
  64.                   subnum  = SUBNUM(selection);
  65.                   flags = ((struct MenuItem *)ItemAddress(SpadesMenu,
  66.                            (LONG)selection))->Flags;
  67.                   switch(menunum)
  68.                   {
  69.                      case 0:     /* Project menu */
  70.                         switch(itemnum)
  71.                         {
  72.                            case 0:     /* New Game */
  73.                               NewFlag = TRUE;
  74.                               break;
  75.                            case 1:     /* Save Hand */
  76.                               if (flags&CHECKED)
  77.                                     SaveHand = TRUE;
  78.                               else
  79.                                     SaveHand = FALSE;
  80.                               break;
  81.                            case 2:     /* Print Hand */
  82.                               break;
  83.                            case 3:     /* About */
  84.                               (void)EasyRequest(Wdw,&AboutES,NULL,NULL);
  85.                               break;
  86.                            case 4:     /* Quit */
  87.                               QuitFlag = TRUE;
  88.                               break;
  89.                            default:
  90.                               break;
  91.                         }
  92.                         break;
  93.                      case 1:     /* Game menu */
  94.                         switch(itemnum)
  95.                         {
  96.                            case 0:     /* Bags */
  97.                               if (flags&CHECKED)
  98.                                     BagsRule = TRUE;
  99.                               else
  100.                                     BagsRule = FALSE;
  101.                               break;
  102.                            case 1:     /* Nil */
  103.                               if (flags&CHECKED)
  104.                                     NilRule = TRUE;
  105.                               else
  106.                                     NilRule = FALSE;
  107.                               break;
  108.                            case 2:     /* Suggest */
  109.                               Button = MRIGHT;
  110.                               gotmouse = TRUE;
  111.                               break;
  112.                            default:
  113.                               break;
  114.                         }
  115.                         break;
  116.                      default:
  117.                         break;
  118.                   }
  119.                   selection = ((struct MenuItem *)
  120.                               ItemAddress(SpadesMenu,(LONG)selection))
  121.                               ->NextSelect;
  122.                }
  123.                break;
  124.                
  125.                 case MOUSEBUTTONS:
  126.                switch(code)
  127.                {
  128.                   case SELECTUP:
  129.                      Button=MLEFT;
  130.                      gotmouse=TRUE;
  131.                      Mx=((SHORT) Message->MouseX);
  132.                      My=((SHORT) Message->MouseY);
  133.                      break;
  134.                   default:
  135.                      break;
  136.                }
  137.     
  138.             default:
  139.                break;
  140.                 
  141.          }  /* end switch */
  142.       }  /* end while message to process */
  143.         
  144.       if (QuitFlag == TRUE)
  145.       {
  146.          WrapUp();
  147.       }
  148.       else if (NewFlag == TRUE)
  149.       {
  150.          Spades();
  151.       }
  152.    }  /* end while not gotmouse */
  153.  
  154. /**********************************************************
  155. * Function: FinishRoutine                                 *
  156. * Parameters: none                                        *
  157. * Return Values: none                                     *
  158. * Purpose: Display final score, ask to play again.        *
  159. **********************************************************/
  160. void FinishRoutine()
  161. {
  162.    BOOL haveinput;
  163.  
  164.    SetRast(RP,BLUP);
  165.    SetAPen(RP,WHTP);
  166.    SetBPen(RP,BLUP);
  167.  
  168.    Move(RP,112,56);
  169.    Text(RP,"FINAL SCORE:",12);
  170.    Move(RP,80,72);
  171.    Text(RP,"YOU:",4);
  172.    Move(RP,184,72);
  173.    Text(RP,"ME:",3);
  174.    SetAPen(RP,YELP);
  175.    itoa(PlayerScore,String);
  176.    Move(RP,116,72);
  177.    Text(RP,String,strlen(String));
  178.    itoa(CompScore,String);
  179.    Move(RP,212,72);
  180.    Text(RP,String,strlen(String));
  181.    
  182.    Move(RP,112,96);
  183.    Text(RP,"PLAY AGAIN ?",12);
  184.    SetAPen(RP,BLKP);
  185.    SetBPen(RP,WHTP);
  186.    Move(RP,112,112);
  187.    Text(RP,"YES",3);
  188.    Move(RP,188,112);
  189.    Text(RP,"NO",2);
  190.    
  191.    haveinput=FALSE;
  192.    while (!haveinput)
  193.    {
  194.       ReadMouse();
  195.       
  196.       /* Check for YES clicked */
  197.       
  198.       if ((Mx>111)&&(Mx<136)&&(My>105)&&(My<114))
  199.          haveinput=TRUE;
  200.          
  201.       /* Check for NO clicked */
  202.       
  203.       if ((Mx>187)&&(Mx<204)&&(My>105)&&(My<114))
  204.          WrapUp();
  205.    }
  206. }
  207.  
  208. /**********************************************************
  209. * Function: GetPlayerCard                                 *
  210. * Parameters: none                                        *
  211. * Return Values: card picked to play                      *
  212. * Purpose: Allow player to pick card to play.             *
  213. **********************************************************/
  214. int GetPlayerCard()
  215. {
  216.    int i,x,card;
  217.   
  218.    /* Let player know that it's his/her turn */
  219.   
  220.    SetBPen(RP,BLUP);
  221.    SetAPen(RP,YELP);
  222.    Move(RP,200,150);
  223.    Text(RP,"PLAY A CARD",11);
  224.   
  225.    /* Loop until we get a good card */
  226.   
  227.    FOREVER
  228.    {
  229.       ReadMouse();                        /* Wait for mouse click */ 
  230.       if (Button==MRIGHT) SuggestCard();  /* Player wants a suggestion */
  231.       if (Button==MLEFT)                  /* Did player pick a card ? */
  232.       {
  233.          for (i=12 ; i>=0 ; i--)          /* Check from right to left */
  234.          {
  235.             x=(i*10)+21;                  /* Set left corner for card */
  236.         
  237.             /* See if clicked inside this card and if card is
  238.                still unplayed */
  239.         
  240.             if ((My<187)&&(My>144)&&(Mx<(x+42))&&(Mx>=x)&&(Hand[0][i]))
  241.             {
  242.                if (ValidCard(i))  /* Was this a playable card ? */
  243.                {
  244.                   card=Hand[0][i]-1;
  245.                   Hand[0][i]=0;   /* Mark card as played */
  246.                   
  247.                   /* Check to see if Spades were broken */
  248.                   
  249.                   if ((card/13)==SPADES) SpadePlayed=TRUE;
  250.             
  251.                   /* Erase suggestion '*' */
  252.             
  253.                   SetAPen(RP,BLUP);
  254.                   SetOPen(RP,BLUP);
  255.                   RectFill(RP,21,136,170,144);
  256.             
  257.                   DrawCard(CardX[0],CardY[0],card); /* Draw played card */
  258.             
  259.                   /* Erase prompt message */
  260.             
  261.                   SetBPen(RP,BLUP);
  262.                   Move(RP,200,150);
  263.                   Text(RP,"           ",11);
  264.             
  265.                   /* Send back played card */
  266.             
  267.                   return(card);
  268.                }
  269.                else     /* chosen card was not valid, need a new card */
  270.                      i=-1;
  271.             }
  272.          }
  273.       }
  274.    }
  275. }
  276.  
  277. /**********************************************************
  278. * Function: ValidCard                                     *
  279. * Parameters: card -- card in player's hand to check      *
  280. * Return Values: was card valid or not?                   *
  281. * Purpose: To determine if the card chosen by the player  *
  282. *   was valid or not.                                     *
  283. **********************************************************/
  284. BOOL ValidCard(card)
  285. int card;
  286. {
  287.    int i,suit,leadsuit;
  288.   
  289.    SuitNumber[DIAMONDS]=0;
  290.    SuitNumber[CLUBS]   =0;
  291.    SuitNumber[HEARTS]  =0;
  292.    SuitNumber[SPADES]  =0;
  293.   
  294.    /* Count number of cards player has in each suit */
  295.   
  296.    for (i=0 ; i<13 ; i++)
  297.    {
  298.       if (Hand[0][i]) SuitNumber[(Hand[0][i]-1)/13]++;
  299.    }
  300.   
  301.    suit=(Hand[0][card]-1)/13; /* Find suit of played card */
  302.   
  303.    if (!TrickLead)            /* Player is leading */
  304.    {
  305.       /* If he didn't lead a spade, it was a good play */
  306.       if (suit!=SPADES) return(TRUE);
  307.     
  308.       /* If he only has spades, he has no choice */ 
  309.       if ((!SuitNumber[0])&&(!SuitNumber[1])&&(!SuitNumber[2]))
  310.             return(TRUE);
  311.     
  312.       /* If spades have been played, he can lead anything */
  313.       if (SpadePlayed) return(TRUE);
  314.     
  315.       /* Must have lead a spade when it was illegal to */
  316.       return(FALSE);
  317.    }
  318.   
  319.    /* Player doesn't lead */
  320.   
  321.    leadsuit=Card[TrickLead]/13;       /* Find suit that was lead */
  322.   
  323.    /* If he played the suit that was lead, he is OK */
  324.    if (suit==leadsuit) return(TRUE);
  325.   
  326.    /* If he didn't have any, he's OK */
  327.    if (!SuitNumber[leadsuit]) return(TRUE);
  328.   
  329.    /* Must have had some but didn't play them */
  330.    return(FALSE);
  331. }
  332.  
  333. /**********************************************************
  334. * Function: GetPlayerBid                                  *
  335. * Parameters: none                                        *
  336. * Return Values: bid -- number of tricks bid              *
  337. * Purpose: Get the human player's bid. Could use gadgets, *
  338. *          but this is easier to program.                 *
  339. **********************************************************/
  340. int GetPlayerBid()
  341. {
  342.    int bid=1,length;
  343.    BOOL havebid=FALSE;
  344.  
  345.    ShowHand();
  346.  
  347.    /* Draw input box */
  348.   
  349.    SetAPen(RP,YELP);
  350.    SetBPen(RP,BLKP);
  351.    Move(RP,258,142);
  352.    Text(RP,"BID",3);
  353.    Move(RP,250,150);
  354.    Text(RP,"  +  ",5);
  355.    Move(RP,250,158);
  356.    Text(RP,"   OK",5);
  357.    Move(RP,250,166);
  358.    Text(RP,"  -  ",5);
  359.   
  360.    /* Loop until OK is clicked */
  361.   
  362.    while(!havebid)
  363.    { 
  364.       /* Draw Current Bid */
  365.     
  366.       SetAPen(RP,GRNP);
  367.       SetBPen(RP,BLKP);
  368.       Move(RP,250,158);
  369.       Text(RP,"  ",2);
  370.       itoa(bid,String);
  371.       length=strlen(String);
  372.       Move(RP,(258-(4*length)),158);
  373.       Text(RP,String,length);
  374.     
  375.       /* Wait for Mouse input */
  376.     
  377.       ReadMouse();
  378.       if (Button==MLEFT)   /* Left Button Pressed */
  379.       {
  380.          /* plus sign clicked */
  381.          if ((Mx>265)&&(Mx<274)&&(My>143)&&(My<152)) bid++;
  382.             
  383.          /* OK clicked */
  384.          if ((Mx>273)&&(Mx<290)&&(My>151)&&(My<160)) havebid=TRUE;
  385.          
  386.          /* minus sign clicked */
  387.          if ((Mx>265)&&(Mx<274)&&(My>159)&&(My<168)) bid--;
  388.       }
  389.       if (Button==MRIGHT)  /* Right Button Pressed */
  390.       {
  391.          bid=CalcBid(0);   /* Suggest a Bid */
  392.       }
  393.     
  394.       /* Make sure bid is valid */
  395.     
  396.       if (NilRule)
  397.       {
  398.          if (bid<0) bid=0;
  399.          if (bid>13) bid=13;
  400.       }
  401.       else
  402.       {
  403.          if (bid<1) bid=1;
  404.          if (bid>12) bid=12;
  405.       }
  406.    }
  407.   
  408.    /* Erase Input Box */
  409.   
  410.    SetAPen(RP,BLUP);
  411.    SetOPen(RP,BLUP);
  412.    RectFill(RP,250,135,291,168);
  413.   
  414.    /* Display the bid */
  415.   
  416.    SetAPen(RP,YELP);
  417.    SetBPen(RP,BLUP);
  418.    itoa(bid,String);
  419.    Move(RP,MsgX[0],MsgY[0]);
  420.    Text(RP,String,strlen(String));
  421.   
  422.    /* Send the bid back */
  423.   
  424.    return(bid);
  425. }
  426.